<?php
header('Content-type: text/html;charset=utf-8');

mb_internal_encoding('UTF-8');
//error_reporting(E_ALL|E_STRICT);
//ini_set( 'display_errors', '0' );
ini_set("display_errors","1");
ini_set("error_reporting", E_ALL ^ E_NOTICE);

date_default_timezone_set('Europe/Moscow');
setlocale(LC_ALL, 'ru_RU');

spl_autoload_register( 'autoload' );
function autoload( $class ) {
  if( is_file( 'classes/'.$class.'.class.php' ) ) {
    require_once( 'classes/'.$class.'.class.php' );
  }
  else if(is_file( 'smarty/'.$class.'.class.php' ) ) {
    require_once( 'smarty/'.$class.'.class.php' );
  }
}

App::init();

session_set_cookie_params(0, '/', '', $_SERVER['SERVER_PORT'] == 443, true);
session_start();

try {
  $db = App::get('DB', App::$config['db']);//->init( App::$config['db'] );
  App::get('Router')->init( App::$config['settings']['index'], $db );
  App::get('Auth', $db);
}
catch ( Exception $e ) {
  // mail $e->getTraceAsString();
  $template = 'error';
}

$p = isset( $_GET['p'] ) ? $_GET['p'] : '';
$location = App::get('Router')->getLocation( $p );


$smarty = App::get('Smarty');																										// Настройка Smarty
$smarty->caching = false;																												//
$smarty->plugins_dir[] = 'components';																					//
$smarty->compile_check = true; //false;																					// TODO: зависимость от настроек
$smarty->debugging = false;//true;																							// TODO: зависимость от настроек
$smarty->cache_lifetime = App::$config['settings']['cache_time'];								//

$smarty->assign('title', $location['title'] );																	// TODO
$smarty->assign('keywords', $location['keywords'] );														//
$smarty->assign('description', $location['description'] ); 											//
//$smarty->assign('user', $user->getUser() );																		//

$smarty->assign('date_format', App::$config['settings']['date_format_smarty'] );
$smarty->assign('date_format_full', App::$config['settings']['date_format_full_smarty'] );

App::get('Messages', parse_ini_file( 'configs/messages.ini', true ) )->init($smarty);

$template = '404.tpl';
// проверить существование шаблона и отобразить
if( $smarty->templateExists( $location['link'].'.tpl' ) )
  $template = $location['link'].'.tpl';
else
  header( 'location: /404' );

$smarty->display( $template );
?>